home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_std / comparable.e < prev    next >
Text File  |  1997-04-13  |  3KB  |  123 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. deferred class COMPARABLE
  5.    --
  6.    -- All class handling COMPARABLE object with a total order relation
  7.    -- should inherit from this class.
  8.    --
  9.    
  10. inherit ANY redefine is_equal end;
  11.    
  12. feature 
  13.    
  14.    infix "<" (other: like Current): BOOLEAN is
  15.      -- Is 'Current' strictly less than 'other'?
  16.       require
  17.      other_exists: other /= Void
  18.       deferred
  19.       ensure
  20.      asymmetric: Result implies not (other < Current);
  21.       end;
  22.    
  23.    infix "<=" (other: like Current): BOOLEAN is
  24.      -- Is 'Current' less or equal 'other'?
  25.       require
  26.      other_exists: other /= Void
  27.       do
  28.      Result := not (Current > other)
  29.       ensure
  30.      definition: Result = (Current < other) or is_equal(other);
  31.       end;
  32.  
  33.    infix ">" (other: like Current): BOOLEAN is
  34.      -- Is 'Current' strictly greater than 'other'?
  35.       require
  36.      other_exists: other /= Void
  37.       do
  38.      Result := (other < Current)
  39.       ensure
  40.      definition: Result = (other < Current)
  41.       end;
  42.  
  43.    infix ">=" (other: like Current): BOOLEAN is
  44.      -- Is 'Current' greater or equal than 'other'?
  45.       require
  46.      other_exists: other /= Void
  47.       do
  48.      Result := not (Current < other)
  49.       ensure
  50.      definition: Result = (other <= Current)
  51.       end;
  52.    
  53.    in_range(lower, upper: like Current): BOOLEAN is
  54.          -- Return true if Current is in range [`lower'..`upper']
  55.       do
  56.      Result := (Current >= lower) and then (Current <= upper) 
  57.       ensure
  58.      Result = (Current >= lower and Current <= upper)
  59.       end;
  60.  
  61.    is_equal(other: like Current): BOOLEAN is
  62.       do
  63.      if Current < other then
  64.      elseif other < Current then
  65.      else
  66.         Result := true;
  67.      end;
  68.       ensure then
  69.      trichotomy: Result = (not (Current < other) 
  70.                    and not (other < Current));
  71.       end;
  72.       
  73.    compare(other: like Current): INTEGER is
  74.      -- Compare 'Current' with 'other'.
  75.      -- '<' <==> Result < 0
  76.      -- '>' <==> Result > 0
  77.      -- Otherwise Result = 0.
  78.       require
  79.      other /= Void
  80.       do
  81.      if Current < other then
  82.         Result := -1
  83.      elseif other < Current then
  84.         Result := 1
  85.      end
  86.       ensure
  87.      (Result < 0) = (Current < other);
  88.      (Result = 0) = not (Current < other or Current > other);
  89.      (Result > 0) = (Current > other)
  90.       end;
  91.    
  92.    min(other: like Current): like Current is 
  93.      -- Minimum of 'Current' and 'other'.
  94.       require
  95.      other /= Void
  96.       do
  97.      if Current < other then
  98.         Result := Current
  99.      else
  100.         Result := other
  101.      end;
  102.       ensure
  103.      Result <= Current and then Result <= other;
  104.      compare(Result) = 0 or else other.compare(Result) = 0
  105.       end;
  106.  
  107.    max(other: like Current): like Current is 
  108.      -- Maximum of 'Current' and 'other'.
  109.       require
  110.      other /= Void
  111.       do
  112.      if other < Current then
  113.         Result := Current;
  114.      else
  115.         Result := other;
  116.      end;
  117.       ensure
  118.      Result >= Current and then Result >= other;
  119.      compare(Result) = 0 or else other.compare(Result) = 0
  120.       end;
  121.  
  122. end -- COMPARABLE
  123.